package com.spareparts.activity;

import cm.framework.net.ClientSession;
import cm.framework.net.ControlRunnable;
import cm.framework.net.IErrorListener;
import cm.framework.net.IResponseListener;
import cm.framework.protocol.BaseHttpRequest;
import cm.framework.protocol.BaseHttpResponse;
import cm.framework.protocol.ErrorResponse;

import com.spareparts.R;
import com.spareparts.activity.base.BaseActivity;
import com.spareparts.protocol.LoginRequest;
import com.spareparts.protocol.LoginResponse;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends BaseActivity 
{

	private EditText mUserName;
	private EditText mUserPwd;
	private Button mButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		requestWindowFeature( Window.FEATURE_NO_TITLE );
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_login);
		
		mUserName = (EditText) findViewById(R.id.editTextUserName);
		mUserPwd = (EditText) findViewById(R.id.editTextUserPwd);
		mButton = (Button) findViewById(R.id.buttonLogin);
		
		mButton.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
//				Intent intent = new Intent(LoginActivity.this,MainActivity.class);
//				startActivity(intent);
				String userName = mUserName.getText().toString();
				String userPwd = mUserPwd.getText().toString();
				
				if(null == userName || "".equals(userName.trim()))
				{
					mHandler.sendEmptyMessage(2);
					return ;
				}
				if(null == userPwd || "".equals(userPwd.trim()))
				{
					mHandler.sendEmptyMessage(3);
					return ;
				}
				validate(userName.trim(),userPwd.trim());
				
			}
		});
	}
	
	
	private void validate(String userName,String UserPwd)
	{
		ClientSession.getInstance().asynGetResponse(new LoginRequest(userName,UserPwd),
				new IResponseListener()
				{
					@Override
					public void onResponse(BaseHttpResponse arg0, BaseHttpRequest arg1,ControlRunnable arg2) 
					{
						LoginResponse loginResponse = (LoginResponse) arg0;
						if (null != loginResponse.mUserInfo) 
						{
							mCommonApplication.mUserInfo = loginResponse.mUserInfo;
							Intent intent = new Intent(LoginActivity.this,MainActivity.class);
//							Intent intent = new Intent(LoginActivity.this,WarehouseActivity.class);
							startActivity(intent);
							finish();
						}
						else 
						{
							mHandler.sendEmptyMessage(1);
						}
					}
		},
		new IErrorListener() {
			@Override
			public void onError(ErrorResponse arg0, BaseHttpRequest arg1,
					ControlRunnable arg2) 
			{
				if(null != arg0 && null != arg0.getErrorDesc() && !arg0.getErrorDesc().trim().equals(""))
				{
					Message msg = new Message();
					msg.what = 4;
					msg.obj = arg0.getErrorDesc();
					mHandler.sendMessage(msg);
				}
			}
		}
	);
	}
	
	private Handler mHandler = new Handler()
	{
		@Override
		public void dispatchMessage(Message msg) 
		{
			super.dispatchMessage(msg);
			
			switch(msg.what)
			{
				case 1:
					//用户名或密码错误
					showToast(R.string.login_validate);
					break;
				case 2:
					//用户名不能为空
					showToast(R.string.login_username_notnull);
					break;
				case 3:
					//密码不能为空
					showToast(R.string.login_userpwd_notnull);
					break;
				case 4:
					String str = msg.obj.toString();
					showToast(str);
					break;
			}
		}
	};
	
}
